@echo off

rem ###########
rem goto actual path
rem ###########
set PATH_EXECUTING=%CD%
cd %~dp0

rem ###########
rem init variables
rem ###########
rem parameters
set PROJECT_SWF=default.swf
set PROJECT_NAME=default
set PROJECT_SRC=src
rem read params
if not "%1"=="" set PROJECT_SWF=%1
if not "%2"=="" set PROJECT_NAME=%2
if not "%3"=="" set PROJECT_SRC=%3

rem vars
set PATH_SOURCE=%PATH_EXECUTING%\%PROJECT_SRC%
set FILE_DEBUG=debug.txt
set FILE_BUILDNR=%PROJECT_SWF%.buildnr
set FILE_VERSION=version.txt
set FILE_PRODUCTVERSION=%PROJECT_SWF%.productversion
set NAME_AS3CLASS=FnfoVersion_%PROJECT_NAME%
set FILE_AS3CLASS=%PATH_SOURCE%\%NAME_AS3CLASS%.as

rem ###########
rem buildnumber
rem ###########
if exist %FILE_BUILDNR% (
for /f %%i in (%FILE_BUILDNR%) do set build=%%i
) Else (
	set build=0
)
set /a BUILD+=1
echo %BUILD% > %FILE_BUILDNR%

rem /../../guavestudios.com

rem #### ######
rem revision
rem ###########
echo $WCREV$ > rev.a
SubWCRev .. rev.a rev.txt >> %FILE_DEBUG%
IF EXIST rev.txt (
	for /f %%i in (rev.txt) do set REVISION=%%i
) Else (
	set REVISION=0
)
del rev.txt
del rev.a

rem ############
rem version
rem ############
if not exist %FILE_PRODUCTVERSION% (
	echo 0.0.1>%FILE_PRODUCTVERSION%
	set VERSION=0.0.1
) Else (
	for /f %%i in (%FILE_PRODUCTVERSION%) do set VERSION=%%i
)


rem ###########
rem store information to file
rem ###########
echo ProjectName: %PROJECT_NAME% > %FILE_VERSION%
echo VersionNumber: %VERSION%.%REVISION%-%BUILD% >> %FILE_VERSION%
echo BuildTime: %date% %time% >> %FILE_VERSION%
echo BuildComputer: %COMPUTERNAME%/%USERNAME% >> %FILE_VERSION%


rem ###########
rem generate classfile
rem ###########
if not exist %FILE_AS3CLASS% (
	rem write only if not already esists
	echo //DONT MODIFY THIS FILE IS GENERATED BY VERSION.BAT ^(FNFO^) > %FILE_AS3CLASS%
	echo package { >> %FILE_AS3CLASS%
	echo 	public class %NAME_AS3CLASS% { >> %FILE_AS3CLASS%
	echo 		[Embed^(source='../scripts/version.txt',mimeType='application/octet-stream'^)] >> %FILE_AS3CLASS%
	echo 		public static const VERSION_FILE:Class; >> %FILE_AS3CLASS%
	rem echo 		public function %NAME_AS3CLASS% {} >> %FILE_AS3CLASS%
	echo 	} >> %FILE_AS3CLASS%
	echo } >> %FILE_AS3CLASS%
)

rem ###########
rem final output
rem ###########
echo Fnfo: BuildTime: %date% %time% VersionNumber: %VERSION%.%REVISION%-%BUILD%

del %FILE_DEBUG%
:exit